﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OnATous.DataAccess
{
    public class User
    {
        public bool createUser(string firstName, string lastName, string photo)
        {
            DBO.User user = new DBO.User(0, firstName, lastName, photo, 10, DateTime.Now);
            
            try
            {
                OnATousEntities model = new OnATousEntities();
                model.AddToT_User(ToT_User(user));
                model.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        public bool createUser(DBO.User user)
        {
            user.Credits += 5;
            try
            {
                OnATousEntities model = new OnATousEntities();
                model.AddToT_User(ToT_User(user));
                model.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private T_User ToT_User(DBO.User user)
        {
            T_User t_user = new T_User();
            if (user.Id != 0)
                t_user.Id = user.Id;
            t_user.FirstName = user.FirstName;
            t_user.LastName = user.LastName;
            t_user.Pseudo = user.Pseudo;
            t_user.Photo = user.Photo;
            t_user.Credits = user.Credits;
            t_user.CreditDate = user.CreditDate;
            return t_user;
        }

        public bool UpdateUser(DBO.User user)
        {
            try
            {
                OnATousEntities model = new OnATousEntities();
                T_User t_user = model.T_User.Where(x => x.Id == user.Id).FirstOrDefault();
                if (t_user != null)
                {
                    if (user.FirstName != t_user.FirstName)
                        t_user.FirstName = user.FirstName;
                    if (user.LastName != t_user.LastName)
                        t_user.LastName = user.LastName;
                    if (user.Pseudo != t_user.Pseudo)
                        t_user.Pseudo = user.Pseudo;
                    if (user.Photo != t_user.Photo)
                        t_user.Photo = user.Photo;
                    if (t_user.CreditDate.Date != DateTime.Now.Date)
                    {
                        t_user.Credits += 5;
                        t_user.CreditDate = DateTime.Now;
                    }
                    user.Credits = t_user.Credits;
                    model.SaveChanges();
                    return true;
                }
                else
                {
                    return createUser(user);
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        public DBO.User GetFriendToEval(DBO.User user, List<DBO.User> friends)
        {
            Random rand = new Random((int)DateTime.Now.Ticks);
            DBO.User friend1 = null;
            if (friends.Count != 0)
                friend1 = friends[rand.Next(0, friends.Count - 1)];
            
            while (friends.Count > 0)
            {
                DBO.User friend = friends[rand.Next(0, friends.Count - 1)];
                OnATousEntities model = new OnATousEntities();
                T_Rating t_rating = model.T_Rating.Where(x => x.IdUser == user.Id && x.IdContact == friend.Id).FirstOrDefault();
                if (t_rating != null)
                    friends.Remove(friend);
                else
                   return friend;
            }
            return friend1;
        }

        public DBO.User GetUserById(Int64 id)
        {
            OnATousEntities model = new OnATousEntities();
            T_User user = model.T_User.Where(x => x.Id == id).FirstOrDefault();
            return new DBO.User(user.Id, user.FirstName, user.LastName, user.Photo, user.Credits, user.CreditDate);
        }
    }
}